<?php
/**
*作用:
*说明:
*版权:
*作者:Red	QQ:316765128
*时间:2011/7/28
**/
include_once './include/init.inc.php';
include_once './include/pinyin.func.php';

if(empty($app_openid)){
	//exit('error!');
	//red  app_openid
	//E45997E18E490C586A100D3826B951CF
}

//验证qq用户是否合法
if(!empty($app_openid) && !empty($app_openkey)){
	$re = $qplus->checkLogin($app_openid,$app_openkey);	/* {"ret":0,"valide":1}	valide=>1 有效，0无效   {"ret":1} 返回错误 */
	$re = (array)$re;
	if(!empty($re) and $re['ret']=='0' and $re['valide']=='1'){
		$dateline = time();
		$u = $db -> fetch_one("select `id` from `info_link_user` where `app_openid`='$app_openid'");
		if(empty($u)){	//获取用户信息并插入用户表
			$qu = $qplus -> getUserInfo($app_openid,$app_openkey);
			$qu = (array)$qu;
			if(!empty($qu) and $qu['ret']=='0'){
				$qu['info'] = (array)$qu['info'];
				$nick = $qu['info']['nick'];
				$gender = $qu['info']['gender'];
				$face = $qu['info']['face'];
			}
			$db -> query("insert into `info_link_user`(`app_openid`,`app_openkey`,`nick`,`gender`,`face`,`lastlogin`,`dateline`) values('$app_openid','$app_openkey','$nick','$gender','$face','$dateline','$dateline')");
			$u['id'] = $db -> insert_id();
		}else{	//更新登录时间
			//$qu = $qplus -> getUserInfo($app_openid,$app_openkey);
			$db -> query("update `info_link_user` set `lastlogin`='$dateline' where `id`='".$u['id']."'");
		}
		cookie('user_id',$u['id'],240000);
	}else{ //参数来源不合法,可要求重新登录
		exit();
	}
}

//red add
$cp = empty($_GET['cp'])?'a':strtolower($_GET['cp']);
$dp = empty($_GET['dp'])?'a':strtolower($_GET['dp']);
if(strlen($cp) != 1 or strlen($dp) != 1){
	exit('error!');
}
$dog_all = $db->fetch_all("select `goods_name`,`goods_id`,`original_img` from `info_goods` WHERE cat_id in ('2','64','65','66')");
$dogs = array();
$i=0;
foreach($dog_all as $key=>$val){
	$pinyin = Pinyin($val['goods_name']);
	$firstWord = substr($pinyin, 0, 1);
	if($dp == $firstWord or ($dp == 'z' and $val['goods_name'] == '藏獒')){
		if($dp == 'c' and $val['goods_name'] == '藏獒'){
			
		}else{
			$attr = array();
			$attr = get_goods_properties($val['goods_id'],$db); //属性
			$val['attr'] = @array_reverse($attr['pro']['商品属性']);
			$val['attr'] = @array_slice($val['attr'], 0, 4);
			if($i==4){
				$i = 0;
			}
			$val['num'] = $i;
			$dogs[] = $val;
			++$i;
		}
	}
}
$smarty->assign('dogs',       $dogs);
$smarty->assign('dog_num',       count($dog_all));

$cat_all = $db->fetch_all("select `goods_name`,`goods_id`,`original_img` from `info_goods` WHERE cat_id in ('3')");
$cats = array();
$i=0;
foreach($cat_all as $key=>$val){
	$pinyin = Pinyin($val['goods_name']);
	$firstWord = substr($pinyin, 0, 1);
	if($cp == $firstWord){
		$attr = array();
		$attr = get_goods_properties($val['goods_id'],$db); //属性
		$val['attr'] = @array_reverse($attr['pro']['商品属性']);
		$val['attr'] = @array_slice($val['attr'], 0, 4);
		if($i==4){
			$i = 0;
		}
		$val['num'] = $i;
		$cats[] = $val;
		++$i;
	}
}
$smarty->assign('cats',       $cats);
$smarty->assign('cat_num',       count($cat_all));

$pins = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$dog_pin = $cat_pin = '';
if(empty($_GET['cp'])){
	$_GET['cp'] = 'A';
}
if(empty($_GET['dp'])){
	$_GET['dp'] = 'A';
}
foreach($pins as $key=>$val){
	if($_GET['dp']==$val){
		$dog_pin .= '<li class="selebg">'.$val.'</li>';
	}elseif(in_array($val, array('E','I','O','U','V'))){
		$dog_pin .= '<li style="color:#ccc">'.$val.'</li>';
	}else{
		$dog_pin .= '<li><a href="./index.php?dp='.$val.'&cp='.$_GET['cp'].'">'.$val.'</a></li>';
	}
}
foreach($pins as $key=>$val){
	if($_GET['cp']==$val){
		$cat_pin .= '<li class="selebg">'.$val.'</li>';
	}elseif(in_array($val, array('C','F','G','I','M','P','Q','U','V','W'))){
		$cat_pin .= '<li style="color:#ccc">'.$val.'</li>';
	}else{
		$cat_pin .= '<li><a href="./index.php?dp='.$_GET['dp'].'&cp='.$val.'#cat">'.$val.'</a></li>';
	}
}
$smarty->assign('dog_pin',       $dog_pin);
$smarty->assign('cat_pin',       $cat_pin);
//red end

//
//openid:6D61E701EE71C2DB8A4349E06037697B   用户唯一标识
//token:4779433470145743562   具有访问权限的token
//secret:hMxbVBnciauf89h8   密钥

$smarty->assign('nav','daquan');
fheader();
$smarty->display('baidu.dwt');
ffooter();